package com.demo2do.fms.admin.service.data.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.data.RepairFeeDao;
import com.demo2do.fms.admin.entity.report.data.RepairFeeReportSearch;
import com.demo2do.fms.admin.handler.support.FmsJdbcSupport;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.data.RepairFeeService;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.business.VehicleFeature;
import com.demo2do.fms.entity.data.cost.RepairFee;

/**
 * 
 * @author Wy
 *
 */
@Service("repairFeeService")
public class RepairFeeServiceImpl extends DataServiceImpl<RepairFee> implements RepairFeeService{
    
    @Dao
    private RepairFeeDao repairFeeDao;
    
    @Autowired
    private FmsJdbcSupport fmsJdbcSupport;
    /*
    * (non-Javadoc)
    * @see com.demo2do.fms.admin.service.data.RepairFeeService#count(java.util.List)
    */
    public int count(List<Long> orgIds) {
        
        if(CollectionUtils.isEmpty(orgIds)) {
            return 0;
        }
        
        return repairFeeDao.count(orgIds);
        
   }

    /*
    * (non-Javadoc)
    * @see com.demo2do.fms.admin.service.data.RepairFeeService#list(java.util.List, com.demo2do.core.entity.page.Page)
    */
    public List<RepairFee> list(List<Long> orgIds, Page page) {
        List<RepairFee> result = repairFeeDao.list(orgIds, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RepairFeeService#list(com.demo2do.fms.entity.organization.Organization, com.demo2do.fms.entity.data.business.VehicleFeature)
     */
    public List<RepairFee> list(Organization organization, VehicleFeature vehicleFeature) {
        return repairFeeDao.list(organization.getId(), vehicleFeature.getId());
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RepairFeeService#edit(com.demo2do.fms.entity.organization.Organization, com.demo2do.fms.entity.data.business.VehicleFeature, java.util.List, java.util.List)
     */
    public void edit(Long operator , Organization organization, VehicleFeature vehicleFeature, List<Integer> rangeFroms, List<BigDecimal> prices) {
        
        // 1. disable all the repair fee according to organization and vehicle_feature
        repairFeeDao.disable(organization.getId(), vehicleFeature.getId());
        
        // 2. create new repair fee records
        if(CollectionUtils.isNotEmpty(rangeFroms) && CollectionUtils.isNotEmpty(prices)){
            
            for(int i = 0; i < rangeFroms.size(); i++){
                
                Integer rangeFrom = rangeFroms.get(i);
                BigDecimal price = prices.get(i);
                
                Integer rangeTo = (rangeFroms.size() -1 == i ) ? -1 : (rangeFroms.get(i + 1) -1);
                
                RepairFee repairFee = new RepairFee(rangeFrom, rangeTo, price, organization, vehicleFeature);
                
                genericDaoSupport.save(repairFee.init().operator(operator));
            }
            
        }
        
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.RepairFeeService#exportAll(com.demo2do.fms.admin.entity.report.data.RepairFeeReportSearch)
     */
    public List<Map<String, Object>> exportAll(RepairFeeReportSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        RowMapper<Map<String, Object>> rowMapper = search.buildRowMapper();
        return fmsJdbcSupport.queryForList(sentence, parameters, rowMapper);
    }

}
